package dst.ass2.ejb.session;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.ws.Action;
import javax.xml.ws.FaultAction;
import javax.xml.ws.soap.Addressing;

import dst.ass1.jpa.model.ITaskProcessing;
import dst.ass1.jpa.model.IWorkPlatform;
import dst.ass2.ejb.dto.StatisticsDTO;
import dst.ass2.ejb.session.exception.AssignmentException;
import dst.ass2.ejb.session.exception.WebServiceException;
import dst.ass2.ejb.session.interfaces.ITaskStatisticsBean;
import dst.ass2.ejb.ws.Constants;
import dst.ass2.ejb.ws.IGetStatsRequest;
import dst.ass2.ejb.ws.IGetStatsResponse;
import dst.ass2.ejb.ws.impl.GetStatsRequestAdapter;
import dst.ass2.ejb.ws.impl.GetStatsResponse;
import dst.ass2.ejb.ws.impl.GetStatsResponseAdapter;


/**
 * 2.2.a Service Implementation
 * @author Markus
 *
 */

@Remote(ITaskStatisticsBean.class)
@Stateless
@Addressing
@WebService(serviceName = Constants.SERVICE_NAME,
            name = Constants.NAME,
            targetNamespace = Constants.NAMESPACE,
            portName = Constants.PORT_NAME)
public class TaskStatisticsBean implements ITaskStatisticsBean {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
    @Action(input = "http://localhost:8080/" + Constants.SERVICE_NAME + "/input",
            output = "http://localhost:8080/" + Constants.SERVICE_NAME + "/output",
            fault = { @FaultAction(className = AssignmentException.class, value = "http://localhost:8080/" + Constants.SERVICE_NAME + "/input") })
    @WebMethod(operationName = "getStatisticsForPlatform")
    public @XmlJavaTypeAdapter(GetStatsResponseAdapter.class) IGetStatsResponse getStatisticsForPlatform (
            @XmlJavaTypeAdapter(GetStatsRequestAdapter.class) @WebParam(partName = "request") IGetStatsRequest request,
            @WebParam(header = true, partName = "name") String name) throws WebServiceException {

		if (request == null || name == null) {
	        throw new WebServiceException("Invalid arguments");
	    }
		
		//get WorkPlatform with name
        List<IWorkPlatform> platforms = entityManager.createQuery("SELECT w FROM WorkPlatform w WHERE w.name = :pfname", IWorkPlatform.class)
                .setParameter("pfname", name).getResultList();
        
        StatisticsDTO statisticDTO=null;
        
        //test if exists
        if(platforms==null || platforms.isEmpty()){
        	throw new WebServiceException("UnknownEntityFault");
        }
        else{	
	        statisticDTO = new StatisticsDTO();
	        statisticDTO.setName(name);  //name of workPlatform
	
	        //select all TaskProcessing from the specific WorkPlatform
	        List<ITaskProcessing> processings = entityManager
	                .createQuery("SELECT t FROM TaskProcessing t JOIN t.taskWorkers w JOIN w.taskForce f JOIN f.workPlatform p WHERE p.name = :pfname" , ITaskProcessing.class)
	                .setParameter("pfname", name)
	                .setMaxResults(request.getMaxProcessings())   //maximals requesting 
	                .getResultList();
	        
	        for (ITaskProcessing tp : processings) {
	        	statisticDTO.addProcessing(tp);		//add TaskProcessings of Platform
	        }
        }

        return new GetStatsResponse(statisticDTO);
	}

}
